﻿using ServerControl.Model;
using ServerControl.Model.Enum;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerControl.Dal
{
    public class LogDal : DbBase
    {
        public static int Add(List<Log> logs)
        {
            return Instance.Insertable(logs).ExecuteCommand();
        }

        public static bool Add(Log log)
        {
            return Instance.Insertable(log).ExecuteCommand() > 0;
        }

        public static List<Log> Select(ref int records, int pageIndex, int pageSize, string userName, string account = "", DateTime? start = null, DateTime? end = null)
        {
            records = 0;
            var logs = Instance
                .Queryable<Log>()
                .Where(l => l.UserName == userName)
                .WhereIF(!string.IsNullOrEmpty(account), l => l.Account == account)
                .WhereIF(start.HasValue, l => l.AddTime >= start.Value)
                .WhereIF(end.HasValue, l => l.AddTime < end.Value)
                .OrderBy(l => l.AddTime, OrderByType.Desc)
                .ToPageList(pageIndex, pageSize, ref records);
            return logs;
        }
    }
}
